package org.stone.project.cacheRedis.aop;//package org.stone.project.cacheRedis.aop;
//
//import cn.hutool.core.util.ObjectUtil;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.expression.Expression;
//import org.springframework.expression.spel.standard.SpelExpressionParser;
//import org.springframework.expression.spel.support.StandardEvaluationContext;
//import org.springframework.stereotype.Component;
//import org.stone.common.pojo.annotation.LockCache;
//import org.stone.common.pojo.constant.AopExecOrder;
//import org.stone.common.pojo.exception.ServiceException;
//
///**
// * 锁Aop
// */
//@Aspect
//@Component
//@Order(value = AopExecOrder.LOCK_ORDER)
//public class LockAop {
//    @Autowired
//    private RedissonClient redissonClient;
//
//    /**
//     * 锁
//     * @param joinPoint
//     * @param lockCache
//     * @return
//     */
//
//    @Around(value = "@annotation(lockCache)")
//    public Object addCacheAround(ProceedingJoinPoint joinPoint, LockCache lockCache){
//        //获取参数
//        String res= null;
//        Object[] args = joinPoint.getArgs();
//        String lockKey = lockCache.value();
//        if (args.length > 0){
//            SpelExpressionParser parse = new SpelExpressionParser();
//            StandardEvaluationContext context = new StandardEvaluationContext();
//
//            {
//                MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//                String[] parameterNames = signature.getParameterNames();
//                for (int i = 0; i < parameterNames.length; i++) {
//                    context.setVariable(parameterNames[i],args[i]);
//                }
//            }
//
//            Expression expression = parse.parseExpression(lockKey);
//            lockKey = (String)(expression.getValue(context));
//        }
//        if (ObjectUtil.isEmpty(lockKey)){
//            throw new ServiceException("锁key不能为空");
//        }
//        if (lockCache.addUserId()){
//            String userId = SecurityAdapterUtil.getUserId();
//            lockKey += ":" + userId;
//        }
//
//        RLock lock = null;
//        try {
//            lock = redissonClient.getLock(lockKey);
//            if (lock.tryLock())
//                return joinPoint.proceed(args);
//            else
//                throw new ServiceException(lockCache.errorMsg());
//        } catch (Throwable e) {
//            e.printStackTrace();
//            throw new ServiceException(e.getMessage());
//        }finally {
//            lock.unlock();
//        }
//    }
//}
